<?php

namespace Payment\Common\MiPay\Data\Charge;

use Payment\Common\PayException;
use Payment\Utils\ArrayUtil;

/**
 * Class MiChargeData
 * 通联支付
 *
 * @property string $openid  trade_type=JSAPI，此参数必传，用户在商户appid下的唯一标识
 * @property string $sub_appid 微信分配的子商户公众账号ID
 * @property string $sub_mch_id 	微信支付分配的子商户号
 * @property string $sub_openid 用户在子商户appid下的唯一标识
 *
 *
 * @author yeran
 */
class MiChargeData extends ChargeBaseData
{
    protected function checkDataParam()
    {
        parent::checkDataParam(); // TODO: Change the autogenerated stub

        // 小程序支付,必须设置openid
        $acct = $this->openid || $this->sub_openid;
        if (empty($acct)) {
            throw new PayException('用户唯一标识，必须携带');
        }
    }

    /**
     * 组装数据，用于计算sign，以及与支付平台对接
     */
    protected function buildData()
    {
        $signData = [
            // 基本数据
            'appid' => trim($this->appId),
            'sub_merchant_id'=> $this->sub_merchant_id,
            'sub_appid'     => trim($this->sub_appid),
            'method'        => 'trade.wxpay.unifiedorder',
            'version'       => $this->version,
            'nonce_str'     => $this->nonce_str,
            'trade_type'    => $this->trade_type,
            'notify_url'    => $this->notifyUrl,
            'limit_pay'     => $this->limitPay,  // 指定不使用信用卡
            'out_trade_sn'  =>$this->out_trade_sn,
            'sign_type'     => $this->sign_type,
            'body'          => trim($this->body),
            'attach'        => trim($this->attach),
            'sub_openid'    => trim($this->sub_openid),
            'total_amount'  => $this->total_amount,//单位分
            'spbill_create_ip' => $this->spbill_create_ip,
        ];

        if($this->time_expire){
            $signData['time_expire']   = $this->time_expire; //订单失效时间，格式为yyyyMMddHHmmss
        }

        // 移除数组中的空值
        $this->retData = ArrayUtil::paraFilter($signData);

    }
}
